package dk.itu.bmds.baywatch.android;

import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.ViewGroup;

public class ViewPhotoActivity extends Activity implements View.OnClickListener {
	static final int DIALOG_DOWNLOADING = 0;
	
	String photoUri;
	String pageUri;
	
	@Override
	protected Dialog onCreateDialog(int id) {
		if(DIALOG_DOWNLOADING == id) {
		    return ProgressDialog.show(this, "", "Downloading photo, please wait...", true);
		}
		return super.onCreateDialog(id);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView( R.layout.photo );
		
		Intent startIntent = getIntent();
		photoUri = startIntent.getStringExtra(ViewThumbnailsActivity.EXTRA_PHOTO_URI);
		pageUri = startIntent.getStringExtra(ViewThumbnailsActivity.EXTRA_PHOTO_PAGE_URI);
		
		new PhotoDownloadAsyncTask().execute(photoUri);
	}
	

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse(pageUri));
		startActivity(intent);
	}
	
	protected void setPhoto(Bitmap bitmap){
		ViewGroup parentView = (ViewGroup)findViewById( R.id.PhotoScrollView );
		
		ImageButton img = new ImageButton(this);
		img.setImageBitmap(bitmap);
		img.setOnClickListener(this);

		// add the image button to the parent group
		parentView.removeAllViews();
		parentView.addView(img, bitmap.getWidth(), bitmap.getHeight());
	}

	class PhotoDownloadAsyncTask extends AsyncTask<String,Void,Bitmap> {

		@Override
		protected void onPostExecute(Bitmap result) {
			//super.onPostExecute(result);
			// dismiss the dialog
			dismissDialog(DIALOG_DOWNLOADING);
			if(result == null) {
			    // an error occurred
			    Toast.makeText(ViewPhotoActivity.this, "Can't download the photo!", 10);
			} else {
			    // set the photo
			    setPhoto(result);
			}
		}

		@Override
		protected void onPreExecute() {
			//super.onPreExecute();
			showDialog(DIALOG_DOWNLOADING);
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			Bitmap out = null;
			
			try {
			    // like for the thumbnails, download the image in a Bitmap object
			    URL url = new URL( params[0] );
			    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			    out = BitmapFactory.decodeStream(urlConnection.getInputStream());
			} catch(Exception e) {
			    Log.e("ViewPhotoActivity", "can't download the photo!", e);
			}
			
			return out;
		}

	}
}
